package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.pojo.User;
import com.example.demo.service.impl.UserServiceImpl;
import com.example.demo.unique.annotation.UniqueFlag;
import com.example.demo.unique.enums.UniqueWay;
import org.springframework.web.bind.annotation.*;


/**
 * @version 1.0
 * @Author: LJP
 * @Description: 用户Controller
 * @Date: Created in 12:04 2021/11/26
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private final UserServiceImpl userServiceimpl;

    public UserController(UserServiceImpl userServiceimpl) {
        this.userServiceimpl = userServiceimpl;
    }

    @PostMapping
    @UniqueFlag(value = UniqueWay.INSERT)
    public User insertUser(@RequestBody User user) {
        boolean saveFlag = userServiceimpl.save(user);
        return saveFlag ? userServiceimpl.getById(user.getId()) : null;
    }

    @PutMapping
    @UniqueFlag(value = UniqueWay.UPDATE)
    public User updateUser(@RequestBody User user) {
        boolean updateFlag = userServiceimpl.updateById(user);
        return updateFlag ? userServiceimpl.getById(user.getId()) : null;
    }

    @GetMapping
    public Page<User> getUserByPage(Page<User> page) {
        return userServiceimpl.lambdaQuery().page(page);
    }

}
